package math;

public class SpiralMatrixii59 {
    public int[][] generateMatrix(int n) {
        int left = 0;
        int right = n-1;
        int top = 0;
        int down = n-1;
        int[][] result = new int[n][n];
        int num =1;
        while (left<=right&&top<=down){
            if(left==right||top==down){
                result[left][left]=num++;
            }

            for (int i = left; i <right ; i++) {
                result[top][i]=num++;
            }
            for (int i = top; i <down ; i++) {
                result[i][right]=num++;
            }
            for (int i = right; i > left ; i--) {
                result[down][i]=num++;
            }
            for (int i = down; i > top ; i--) {
                result[i][left]=num++;
            }
            left++;
            right--;
            top++;
            down--;
        }
        return result;
    }


}
